//
//  rightSubDetailContentView.m
//  dataGathering
//
//  Created by apple on 14-11-6.
//  Copyright (c) 2014年 shuili. All rights reserved.
//

#import "rightSubDetailContentView.h"
#import "mainVC.h"
#import "rightSubDetailView.h"

@interface rightSubDetailContentView ()
@property (weak, nonatomic) IBOutlet UISwitch *autoSyncSwitch;
- (IBAction)syncSwitchChanged:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *creatorLabel;
@property (weak, nonatomic) IBOutlet UILabel *createdTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *updatedTimeLabel;
@property (weak, nonatomic) IBOutlet UIScrollView *contentScrollView;
@property (weak, nonatomic) IBOutlet UILabel *locationDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *dataDescLabel;
@end

@implementation rightSubDetailContentView
@synthesize markCompletedButton = _markCompletedButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [_contentScrollView setContentSize:CGSizeMake(387, 829)];
    
    [self setCurrentWorkSetLabels];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setCurrentWorkSetLabels) name:@"updateSelectedWorkSet" object:nil];
    
    // Do any additional setup after loading the view.
}

- (void)setCurrentWorkSetLabels
{
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    workSet *currentWorkSet = [manager getCurrentWorkSet];
    [_creatorLabel setText:[currentWorkSet creator]];
    [_locationDescriptionLabel setText:[currentWorkSet locationString]];
    [_createdTimeLabel setText:[currentWorkSet createTime]];
    [_updatedTimeLabel setText:[currentWorkSet updateTime]];
    [_autoSyncSwitch setOn:[currentWorkSet isAutoSync] animated:YES];
    [_dataDescLabel setText:[currentWorkSet worksetDescription]];
    if([currentWorkSet isCompleted])
    {
        [_markCompletedButton setTitle:@"标记为未完成" forState:UIControlStateNormal];
        [_markCompletedButton setTitle:@"标记为未完成" forState:UIControlStateHighlighted];
    } else {
        [_markCompletedButton setTitle:@"标记为已完成" forState:UIControlStateNormal];
        [_markCompletedButton setTitle:@"标记为已完成" forState:UIControlStateHighlighted];
    }

    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)imageClicked:(id)sender {
    NSLog(@"image clicked");
    [(mainVC*)(self.parentViewController.parentViewController.parentViewController.parentViewController.parentViewController) openDetailMapView];
}
- (IBAction)syncSwitchChanged:(id)sender {
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    workSet *currentWorkSet = [manager getCurrentWorkSet];
    currentWorkSet.isAutoSync = [_autoSyncSwitch isOn];
    [manager updateIsAutoSync:[currentWorkSet name] :[_autoSyncSwitch isOn]];
}
- (IBAction)deleteTaskButtonClicked:(id)sender {
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    [manager markWorksetToDelete];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTaskCollectionTableView" object:nil];
    [((rightSubDetailView*)(self.parentViewController.parentViewController)) removeAdditionalView];
    
}
- (IBAction)markCompletedButtonClicked:(id)sender {
    currentWorkSetManager *manager = [currentWorkSetManager sharedInstance];
    workSet *currentWorkSet = [manager getCurrentWorkSet];

    
    
    if([[_markCompletedButton titleForState:UIControlStateNormal] isEqualToString:@"标记为已完成"])
    {
        [manager updateIsCompleted:[currentWorkSet name] :YES];
        [_markCompletedButton setTitle:@"标记为未完成" forState:UIControlStateNormal];
        [_markCompletedButton setTitle:@"标记为未完成" forState:UIControlStateHighlighted];
    } else {
        [manager updateIsCompleted:[currentWorkSet name] :NO];
        [_markCompletedButton setTitle:@"标记为已完成" forState:UIControlStateNormal];
        [_markCompletedButton setTitle:@"标记为已完成" forState:UIControlStateHighlighted];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshTaskCollectionTableView" object:nil];
    
    
}
@end
